﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ignitron.EEngine.Model;
using SharpDX.Direct3D11;

namespace Ignitron.EEngine.Adapters.DX.Graphics
{
    /// <summary>
    /// Represents a SharpDX texture
    /// </summary>
    internal class SharpDXTexture: ETexture
    {
        private ShaderResourceView mTexture;
        protected Device mDevice;

        /// <summary>
        /// Constructor
        /// </summary>
        public SharpDXTexture(Device device)
        {
            mDevice = device;
        }

        /// <summary>
        /// Loads the texture from a file
        /// </summary>
        public override bool Load(string filePath)
        {
            try
            {
                mTexture = ShaderResourceView.FromFile(mDevice, filePath);
            }
            catch
            {
                return false;
            }

            return true;
        }

        /// <summary>
        /// Shuts down the texture
        /// </summary>
        public virtual void Shutdown()
        {
            if (mTexture != null)
            {
                mTexture.Dispose();
                mTexture = null;
            }
        }

        /// <summary>
        /// Returns the texture resource
        /// </summary>
        internal virtual ShaderResourceView GetResource()
        {
            if (mTexture == null)
                throw new ApplicationException("Texture was not loaded.");
            return mTexture;
        }
    }
}
